﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System.Collections;
using System.Collections.Generic;
using Tesla.Graphics;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics {
    /// <summary>
    /// Concrete XNA 4.0 implementation of <see cref="IEffectParameterCollection"/>.
    /// </summary>
    public sealed class XNAEffectParameterCollection : IEffectParameterCollection {
        private List<XNAEffectParameter> _params;

        /// <summary>
        /// Gets the number of parameters contained in the collection.
        /// </summary>
        public int Count {
            get {
                return _params.Count;
            }
        }

        /// <summary>
        /// Gets the parameter specified by its index.
        /// </summary>
        /// <returns>The parameter, if it exists</returns>
        public IEffectParameter this[int index] {
            get {
                if(index >= 0 && index < _params.Count) {
                    return _params[index];
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the parameter specified by its index.
        /// </summary>
        /// <returns>The parameter, if it exists</returns>
        public IEffectParameter this[string name] {
            get {
                for(int i = 0; i < _params.Count; i++) {
                    IEffectParameter param = _params[i];
                    if(param.Name.Equals(name)) {
                        return param;
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="XNAEffectParameterCollection"/>.
        /// </summary>
        /// <param name="coll">The XNA effect parameter collection to copy from.</param>
        internal XNAEffectParameterCollection(XFG.EffectParameterCollection coll) {
            _params = new List<XNAEffectParameter>(coll.Count);
            for(int i = 0; i < coll.Count; i++) {
                _params.Add(new XNAEffectParameter(coll[i]));
            }
        }

        /// <summary>
        /// Gets the parameter specified by its semantic.
        /// </summary>
        /// <param name="name">Semantic of the parameter</param>
        /// <returns>
        /// The parameter, if it exists
        /// </returns>
        public IEffectParameter GetParameterBySemantic(string name) {
            for(int i = 0; i < _params.Count; i++) {
                IEffectParameter param = _params[i];
                if(param.Semantic.Equals(name)) {
                    return param;
                }
            }
            return null;
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<IEffectParameter> GetEnumerator() {
            return (IEnumerator<IEffectParameter>) _params.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator() {
            return (IEnumerator) _params.GetEnumerator();
        }
    }
}
